<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\controller;

use app\store\model\store\Shop as ShopModel;
use app\common\library\amap\WxShop;
use think\facade\Db;
/**
 * 门店管理
 * Class Shop
 * @package app\store\controller\store
 */
class Shop extends Controller {
	public function roleList() {
	
		$list = Db::name('store_shop_clerk_role')->select()->toArray();
		return $this->renderSuccess(compact('list'));
	}
	/**
	 * 门店列表
	 * @return array
	 * @throws \think\db\exception\DbException
	 */
	public function list() {
		$model = new ShopModel;
		$list = $model->getList($this->request->param());
		return $this->renderSuccess(compact('list'));
	}
	public function listShuju() {
		$model = new ShopModel;
		$list = $model->getListShuju($this->request->param());
		return $this->renderSuccess(compact('list'));
	}
	public function listAll() {
		$model = new ShopModel;
		$list = $model->getListAll($this->request->param());
		return $this->renderSuccess(compact('list'));
	}
	public function listDaili() {
		$model = new ShopModel;
		$list = $model->getListDaili($this->request->param());
		return $this->renderSuccess(compact('list'));
	}
	public function getTypeList() {
		$model = new ShopModel;
		$list = $model->getList($this->request->param());
		return $this->renderSuccess(compact('list'));
	}
	public function getCaiXiList() {
		$model = new ShopModel;
		$list = $model->getList($this->request->param());
		return $this->renderSuccess(compact('list'));
	}
	/**
	 * 用户充值
	 * @param int $userId
	 * @param string $target
	 * @return array
	 */
	public function recharge(int $shopId, string $target) {
		// 用户详情
		$model = ShopModel::detail($shopId);
		if ($model->recharge($target, $this->postForm())) {
			return $this->renderSuccess('操作成功');
		}
		return $this->renderError($model->getError() ?: '操作失败');
	}

	/**
	 * 全部记录
	 * @return array
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function all() {
		$model = new ShopModel;
		$list = $model->getAll($this->request->param());
		return $this->renderSuccess(compact('list'));
	}

	/**
	 * 获取详情记录
	 * @param int $shopId
	 * @return array
	 */
	public function detail(int $shopId) {
		// 获取门店详情
		$detail = ShopModel::detail($shopId, ['types', 'foods', 'images.file', 'vrVideo','advImages.file']);
		// 获取logoImage (这里不能用with因为编辑页需要logoImage对象)
		!empty($detail) && $detail['logoImage'];
		!empty($detail) && $detail['floatImage'];
		!empty($detail) && $detail['vrImage'];
		!empty($detail) && $detail['cardImage'];
		!empty($detail) && $detail['parkImage'];

		return $this->renderSuccess(compact('detail'));
	}
	public function getTime($type = 0) {
		$start_date = Date('2024-12-12 00:00');
		$start_date = strtotime($start_date);
		$time1 = [];
		$time = [];
		for ($i = 1; $i < 31; $i++) {
			$time['label'] = Date('H:i', $start_date);
			$time['value'] = $i;
			$start_date += 30 * 60;
			$time1[] = $time;
			

		}

		$start_date = Date('2024-12-12 15:00');
		$start_date = strtotime($start_date);
		$time2 = [];
		$dd = [];
		for ($i = 1; $i < 19; $i++) {
			$dd['label'] = Date('H:i', $start_date);
			$dd['value'] = $i;
			$start_date += 30 * 60;
			$time2[] = $dd;

		}


		$start_date = Date('2024-12-12 00:00');
		$start_date = strtotime($start_date);

		$time = [];
		for ($i = 1; $i < 49; $i++) {
			$time22['label'] = Date('H:i', $start_date);
			$time22['value'] = $i;
			$start_date += 30 * 60;

			$time[] = $time22;

		}

		return $this->renderSuccess(compact('time', 'time1', 'time2'));
	}
	/**
	 * 添加门店
	 * @return array
	 */
	public function add() {
		// 新增记录
		$model = new ShopModel;
		if ($model->add($this->postForm())) {
			return $this->renderSuccess('添加成功');
		}
		return $this->renderError($model->getError() ?: '添加失败');
	}

	/**
	 * 编辑门店
	 * @param int $shopId
	 * @return array|bool|mixed
	 */
	public function edit(int $shopId) {
		// 门店详情
		$model = ShopModel::detail($shopId);

		// 新增记录
		if ($model->edit($this->postForm())) {
			$biz_content['shop_id']=$shopId;
			$biz_content['status']=1;
			//$biz_content['shop_name']=$this->postForm()['shop_name'];
	
			// $data['status']=1;
			// $data['method']='amap.dining.pushIncrPoi';
		
			$WxShop = new WxShop();
			//$WxShop->updateShop('amap.dining.pushIncrPoi',$biz_content);

			//同步高德，2024-12-28,无权限，注释
			//$WxShop->updateShop('amap.life.pushIncrPoi',$biz_content);

			

			return $this->renderSuccess('更新成功');
		}
		return $this->renderError($model->getError() ?: '更新失败');
	}

	/**
	 * 删除门店
	 * @param int $shopId
	 * @return array
	 */
	public function delete(int $shopId) {
		// 门店详情
		$model = ShopModel::detail($shopId);
		if (!$model->setDelete()) {
			return $this->renderError($model->getError() ?: '删除失败');
		}
		return $this->renderSuccess('删除成功');
	}

	 /**
     * 模拟POST请求 [第二种方式, 用于兼容微信api]
     * @param $url
     * @param array $data
     * @return mixed
     * @throws \cores\exception\BaseException
     */
    protected function post($url, array $data = [])
    {	
    	$data2['utc_timestamp']=time();
    	$data2['sign']='b9bbf58608fe017bf4951db2e868f94d';
    	$data2['sign_type']='RSA2';
    	$data2['app_id']='202409070195789533';
    	$data2['version']='1.0';
    	$data2['charset']='UTF-8';
        $header = [
            'Content-Type: application/x-www-form-urlencoded'
        ];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
        $result = curl_exec($ch);
        if ($result === false) {
            throwError(curl_error($ch));
        }
        curl_close($ch);
        return $result;
    }
}
